package com.kqzz.common.config.mapper;

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import com.kqzz.common.bean.BaseEntity;
import com.kqzz.common.utils.SystemContants;

import cn.hutool.core.date.DateUtil;

/**
 * @Description 数据库通用字段更新处理拦截器
 * @ClassName HandleCommonInterceptor.java
 * @Package com.kqzz.common.config.mapper 
 * @author lipeng
 * @date 2022年1月25日 上午9:13:37
 * 
 * @update_Desc
 * @update_Author
 * @update_Date
 */
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
public class UpdateHandleCommonInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		Object[] args = invocation.getArgs();
		MappedStatement mappedStatement = (MappedStatement) args[0];
		SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
//		String username = SecurityUtils.getUsername();
		String dateTime = DateUtil.format(new Date(),SystemContants.DEFAULT_DATE_TIME);
		if(sqlCommandType == SqlCommandType.UPDATE) {
//			System.err.println(args[1]);
			if(args[1] instanceof BaseEntity) {
				BaseEntity baseEntity = (BaseEntity) args[1];
				baseEntity.setUpdatetime(dateTime);
//				baseEntity.setUpdateby(username);
			 }else if(args[1] instanceof Map){ // 调用mp的更新方法时，参数会变成如下形式：{param1=Receive(id=16, dh=1244, qzh=null, postId=1)} 正常的应该是Receive(id=16, dh=1244, qzh=null, postId=1)
				 Map<String,Object> map = (Map<String, Object>) args[1];
				 Object object = getFirstOrNull(map);
				 if(object instanceof BaseEntity) {
					BaseEntity baseEntity = (BaseEntity) object;
					baseEntity.setUpdatetime(dateTime);
//					baseEntity.setUpdateby(username);
				 }
			 }
		}else if(sqlCommandType == SqlCommandType.INSERT) {
			 if(args[1] instanceof BaseEntity) {
				BaseEntity baseEntity = (BaseEntity) args[1];
				baseEntity.setCreatetime(dateTime);
//				baseEntity.setCreateby(username);
			}
		}
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		// TODO Auto-generated method stub
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties arg0) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * 获取第一个key的值
	 * @param map
	 * @return
	 */
	private static Object getFirstOrNull(Map<String, Object> map) {
        Object obj = null;
        for (Entry<String, Object> entry : map.entrySet()) {
            obj = entry.getValue();
            if (obj != null) {
                break;
            }
        }
        return  obj;
    }
}
